<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/12/4
 * Time: 14:58
 */

namespace app\zyhsapi\model;

use think\Log;
use XznsTools\cache\zyhs\CacheManageZyhs;

class ZyhsQuestionUserView extends BaseCacheZyhs
{
    protected $table = 'good_wechat.zyhs_question_user_view';

    protected $cacheModel = 'question_answer.question_view';

    public function getLookGoodData()
    {
        // 获取配置信息
        $questionConfig = config('zyhs_question');
        $gid = $questionConfig['look_gid'];
        $goodConfig = config('zyhsgoods.qa_goods');
        $nowPrice = $goodConfig[$gid]['now_prize'];

        return [
            'price' => $nowPrice,
            'gid' => $gid
        ];
    }

    /**
     * 问题蹭看
     * @param array $data
     * @return bool
     */
    public function SaveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);
            // 添加至我的问答-蹭看列表
            $this->listDataAdd($data['uid'], [$id => $id]);
            $time = $data['create_time'] ?? time();
            CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('view_task_id', $data['uid'])->listAdd([$data['task_id'] => $time], 0, 'view_task_id');

            return true;
        }
        catch (\Exception $e)
        {
            Log::write('问答 ZyhsQuestionUserView model => SaveData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }
    }

    /**
     * 获取用户 蹭看的任务IDs
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getUserViewQuestionTaskIds(int $uid)
    {
        $resData = CacheManageZyhs::getInstance()
            ->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('view_task_id', $uid)
            ->setPage(-1)
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($uid)
            {
                try
                {
                    $data = $this->where(['uid' => $uid, 'status' => 1])->column('id', 'task_id');
                    $resData = [];
                    foreach ($data as $taskId => $createTime)
                    {
                        $resData[$taskId] = ['id' => $createTime];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0, 'view_task_id');

        return $resData['data'] ? array_keys($resData['data']) : [];
    }

    /**
     * 获取问答 蹭看用户uids
     * @param string $task_id
     * @return array
     * @throws \Exception
     */
    public function getQuestionViewUserIds(string $task_id): array
    {
        $resData = CacheManageZyhs::getInstance()
            ->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('view_user', $task_id)
            ->setPage(-1)
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($task_id)
            {
                try
                {
                    $data = $this->where(['task_id' => $task_id, 'status' => 1])->column('id', 'uid');
                    $resData = [];
                    foreach ($data as $uid => $createTime)
                    {
                        $resData[$uid] = ['id' => $createTime];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0, 'view_user');

        return $resData['data'];
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $id => $questionItemData)
        {
            $resData[$id] = arrayKeyData2int($questionItemData, ['id', 'uid', 'is_hide', 'new_answer', 'status', 'create_time']);
        }

        return $resData;
    }

    /**
     * 批量获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $model = $this->where('id', 'in', $ids);
            } else
            {
                $model = $this->where(['uid' => $type[0], 'is_hide' => 0]);
            }
            $data = $model->order('id', 'desc')->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    /**
     * 获取用户是否蹭看
     * @param int $uid
     * @param string $task_id
     * @return bool
     * @throws \Exception
     */
    public function getQuestionUserIsView(int $uid, string $task_id): bool
    {
        $viewUids = $this->getQuestionViewUserIds($task_id);
        $viewUids = $viewUids ? array_keys($viewUids) : [];

        return in_array($uid, $viewUids, false) ? true : false;
    }

    /**
     * 获取用户是否蹭看及蹭看数
     * @param int $uid
     * @param string $task_id
     * @return array
     * @throws \Exception
     */
    public function getQuestionViewNumAndIsView(int $uid, string $task_id): array
    {
        $viewUids = $this->getQuestionViewUserIds($task_id);
        $viewUids = $viewUids ? array_keys($viewUids) : [];

        return [
            'is_view' => in_array($uid, $viewUids, false) ? true : false,
            'view_num' => count($viewUids)
        ];
    }
}
